const { Http } = require("../../utils/http");
const app = getApp();
import { config } from "../../config/config";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon_id: "",
    topImg:"",
    imgList:[],
    scene:''
  },
  navUrl: function () {
    // wx.navigateTo({
    //   url: '/pages/getcoupon/index?coupon_id=' + wx.getStorageSync('coupon_id'),
    // })
    //U美
     wx.navigateTo({
      url: '/pages/getcoupon/index?scene=' + wx.getStorageSync('sceneCs'),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login((res) => {//页面初始化
      wx.setStorageSync('register', res.register);
      wx.setStorageSync('shoplist', res.store_list);
      wx.setStorageSync('verifyman', res.verification);
      wx.setStorageSync('distributor', res.distributor);
      wx.setStorageSync('user_id', res.user_id);
      wx.setStorageSync('store_id', res.store_id);
      wx.setStorageSync('money', res.userinfo.money);
      wx.setStorageSync('payment_password', res.userinfo.payment_password);
      wx.hideLoading();
      console.log('成功');
      this.location();
      this.imgList();
    });
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //     wx.request({
    //       url: `${config.apiBaseUrl}wechat/index/login`,
    //       data: {
    //         code: res.code,
    //       },
    //       success: (res) => {
    //         // console.log(res);  
    //         wx.setStorageSync('token', res.data.data.token);
    //         // console.log(wx.getStorageSync('token'));
    //       }
    //     });
    //   }
    // })
    console.log(options);
    this.setData({
      coupon_id: options.coupon_id,//只有一个分享按钮时传出的优惠券ID
      scene: options.scene//U美分享出来的场景值
    })
    wx.setStorageSync('coupon_id', options.coupon_id)
    wx.setStorageSync('sceneCs', options.scene)//U美分享出来的场景值
    // console.log(options.coupon_id);
   
  },
  location: async function () {//初始化接口
    let result = await Http.request({
      url: 'wechat/index/init',
      data: { token: wx.getStorageSync('token') },
      method: "GET"
    });
    console.log(result);
    if (result.code == 0) {
      wx.setStorageSync('shoplist', result.data.store_list);
    } else {
      console.log("网络异常,请重试..");
    }
  },
  imgList: async function () {//获取图片
    let result = await Http.request({
      url: 'wechat/product/get_detail_by_id',
      data: { id:12 },
      method: "GET"
    });
    console.log(result);
    if (result.code == 0) {
      this.setData({
        imgList:result.data.arr_new_images,
        topImg:result.data.header_new_images
      })
    } else {
      console.log("网络异常,请重试..");
    }
  }
})